import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * The main class.
 * Both GUI and button functional is implemented.
 */
class SimpleGui1 {

    /* Button for start/stop functional */
    private JButton button;
    /* Button for increase speed functional */
    private JButton incrSpeedButton;
    /* Button for decrease speed functional */
    private JButton decrSpeedButton;
    /* Label with current speed */
    private JLabel speedLabel;
    /* The x step of disc */
    private int dx;
    /* The y step of disc */
    private int dy;
    /* Speed of disc */
    private int v;
    /* The x position of disc */
    private int x = 50;
    /* The y position of disc */
    private int y = 50;
    /* Variable, that permit or prohibit movement of disc */
    private boolean circle = true;
	
	private MyDrawPanel panel; 
    /**
     * This method points start position of disk and passes on.
     */
    void start() {
        dx = 0;
        dy = 0;
        v = 3;
        setUpGui();
		goOn();                         // Begin to move
    }

    /**
     * Frame and button are created and turned on.
     */
    private void setUpGui() {

        panel = new MyDrawPanel();
        JFrame frame = new JFrame();
        speedLabel = new JLabel("Current speed = " + v);
        button = new JButton("Stop");
        incrSpeedButton = new JButton("Increase speed");
        decrSpeedButton = new JButton("Decrease speed");
        Font big = new Font("a",Font.CENTER_BASELINE, 30);
        speedLabel.setFont(big);
        button.setFont(big);
        incrSpeedButton.setFont(big);
        decrSpeedButton.setFont(big);
        button.addActionListener(new StopActionListener());
        incrSpeedButton.addActionListener(new IncrActionListener());
        decrSpeedButton.addActionListener(new DecrActionListener());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel buttonPanel = new JPanel( );
        buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.Y_AXIS));
        buttonPanel.add(speedLabel);
        buttonPanel.add(button);
        buttonPanel.add(incrSpeedButton);
        buttonPanel.add(decrSpeedButton);
        frame.getContentPane().add(BorderLayout.EAST, buttonPanel);

        frame.getContentPane().add(BorderLayout.CENTER, panel);
        frame.setSize(1000, 1000);
        frame.setVisible(true);
        
    }

    /**
     * Method of continuing of movement
     */
    private void goOn (){

        Thread loopThread = new Thread(new Runnable() {         // Here anonymous runnable class is declared to provide work for new thread
            /**
             * Position of disc is calculated and bring it to JPanel inner class.
             * Four points of rotation are detected, where speed changes.
             */
            @Override
            public void run() {                                 // This is the only method of anonymous class
                while (circle) {
                    /* looking for turn points */
                    if (x >= 500) {
                        if (y >= 300) {         //right bottom point
                            dx = -1;
                            dy = 0;
                        } else if (y <= 50) {    //right top point
                            dx = 0;
                            dy = 1;
                        }
                    } else {
                        if (x <= 50) {
                            if (y <= 50) {       //left top point
                                dx = 1;
                                dy = 0;
                            } else if (y >= 300) {      //left bottom point
                                dx = 0;
                                dy = -1;
                            }
                        }
                    }
                    /* Increase of disk coordinates */
                    x += dx;
                    y += dy;
                    panel.repaint();
                    try {
                        Thread.sleep(20 / v);
                    } catch (Exception ex) {
                    }

                }
            }
        });
        loopThread.start();
    }

    /**
     * Class, which makes a picture of disk
     */
    class MyDrawPanel extends JPanel {

        public void paintComponent(Graphics g) {
            g.setColor(Color.black);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());    //Backgroud

            g.setColor(Color.GREEN);
            g.fillOval(x, y, 40, 40);                               //Our disk
        }
    }

    class StopActionListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            circle = !circle;             // Start or stop disk
        /*
        Changes the name of the button and go on, if it's necessary
         */
            if (circle) {
                button.setText("Stop");
                goOn();
            } else {                        // stop case
                button.setText("Start");

            }

        }
    }

    class IncrActionListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            v++;
            speedLabel.setText("Current speed = " + v);
        }
    }
    
    class DecrActionListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            if (v == 1) {
            } else {
                v--;
                speedLabel.setText("Current speed = " + v);
            }
        }
    }
}
